package com.example.demo.service.good.impl;

import com.alibaba.fastjson.JSON;
import com.example.demo.entity.CollectionName;
import com.example.demo.entity.base.Condition;
import com.example.demo.entity.base.Criteria;
import com.example.demo.entity.bo.Good;
import com.example.demo.entity.bo.Supplier;
import com.example.demo.service.comm.AggregationUtil;
import com.example.demo.service.good.GoodService;
import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoClient;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @description:
 * @author: cj
 * @create: 17-03-09 17:06:52
 */
@Service
@Slf4j
public class GoodServiceImpl implements GoodService {
    @Resource
    MongoClient mongoClient;
    @Resource
    MongoTemplate mongoTemplate;

    @Override
    public List<Map> findGoods(Condition condition) {
        //将条件对象转换成查询命令
        Query query = Condition.changeQuery ( condition );
//        mongoClient.getDatabase( "" ).getCollection( "" ).find( "" )
        List<Map> mapList = mongoTemplate.find ( query, Map.class, CollectionName.GOODS );
        //and  ---
        return mapList;
    }

    /**
     * 添加商品
     *
     * @param good 商品数据
     * @return 商品数据
     */
    @Override
    public Map addGood(Good good) {
        good.set_id ( new ObjectId () );
        good.setBarcode ( String.valueOf ( System.nanoTime () ) );
        Good insert = mongoTemplate.insert ( good, CollectionName.GOODS );
        return JSON.parseObject ( JSON.toJSONString ( insert ) );
    }

    /**
     * 获取商品库信息
     *
     * @param map 商品数据 page pageSize
     * @return 商品数据
     */
    @Override
    public Map getGoodBankInfo(Map<String, Object> map) {
        Map<String, Object> queryMap = new HashMap<> ();
        //skip limit group(分组字段) keys（查询出的的别名） cz （操作字段 push addToSet） file (要合并的字段)
        int pageSize = Integer.parseInt ( map.get ( "pageSize" ).toString () );
        int page = Integer.parseInt ( map.get ( "page" ).toString () );
        queryMap.put ( "skip", (page - 1) * pageSize );
        queryMap.put ( "limit", pageSize );
        queryMap.put ( "group", "_id" );
        queryMap.put ( "keys", "names" );
        queryMap.put ( "cz", "$addToSet" );
        queryMap.put ( "file", "$supplier" );
        Aggregation aggregation = Aggregation.newAggregation ( AggregationUtil.mergeToArray ( queryMap ) );

        AggregationResults<Map> aggregate = mongoTemplate.aggregate ( aggregation, CollectionName.GOODS, Map.class );
        Map result = aggregate.getUniqueMappedResult ();


        result.put ( "goodNum", mongoTemplate.count ( Query.query ( new Criteria () ), CollectionName.GOODS ) );
        return result;
    }

    /**
     * 添加供应商信息
     *
     * @param suppliers 供应商信息
     * @return 成功添加的供应商
     */

    public List<Object> addSuppliers(List<Supplier> suppliers, String m) {
        Collection<Supplier> insert = mongoTemplate.insert ( suppliers, CollectionName.SUPPLIER );
        Object[] objects = insert.toArray ();
        return Arrays.asList ( objects );

    }

    /**
     * @param names 添加供应商信息
     * @return 添加供应商信息
     */
    @Override
    public List<Object> addSuppliers(List<String> names) {
        List<Supplier> suppliers = new ArrayList<> ();
        names.forEach ( item -> suppliers.add ( new Supplier ( item ) ) );
        return addSuppliers ( suppliers, "2" );
    }

    /**
     * 矫正
     *
     * @param map 条件 page pageSize condition
     * @return 商品信息
     */
    @Override
    public List<Object> dataCorrect(Map<String, Object> map) {
        List<Map> all = mongoTemplate.findAll ( Map.class, CollectionName.SUPPLIER );
        return null;
    }
}
